import React, { useState,useEffect } from 'react';
import { Form, Input, Modal, Radio, TreeSelect  } from 'antd';
const { TextArea } = Input;
import { FormValueType, TreeNode } from '../data.d';

const FormItem = Form.Item;

export interface CreateFormProps {
  treeData?: TreeNode[];
  modalVisible: boolean;
  onSubmit: (values: FormValueType) => void;
  onCancel: () => void;
  parentId?:number | undefined;
}

const formLayout = {
  labelCol: { span: 5 },
  wrapperCol: { span: 15 },
};

const CreateForm: React.FC<CreateFormProps> = (props) => {


  const [form] = Form.useForm();

  const { modalVisible, onSubmit: handleAdd, onCancel, treeData,parentId } = props;
  console.log(parentId);
  const [formVals,setFormVals] = useState<FormValueType>({
    useState: 1,
    
  });

  const [formParent,setFormParent] = useState<number | undefined>();

  const okHandle = async () => {
    const fieldsValue = await form.validateFields();
    fieldsValue.parentId=formParent;
    if(formParent==null){
      fieldsValue.parentId=parentId;
    }
    
    form.resetFields();
    console.log(fieldsValue)
    handleAdd(fieldsValue);
  };

  const changeParent = (id) => {
    setFormParent(id)
  };

  return (
    <Modal
      width={640}
      destroyOnClose
      title="新增区域"
      visible={modalVisible}
      onOk={okHandle}
      onCancel={() => onCancel()}
    >
      <Form
        {...formLayout}
        form={form}
        initialValues={{
          areaName: formVals.areaName,
          areaDesc: formVals.areaDesc,
          parentId:parentId,
          useState: formVals.useState
        }}
      >
        <FormItem
          label="区域名称"
          name="areaName"
          rules={[{ required: true, message: '请输入至少二个字符！', min: 2 }]}
        >
          <Input placeholder="请输入" autoComplete='off' />
        </FormItem>
        <FormItem
          label="区域描述"
          name="areaDesc"
          rules={[{ min: 5,max:255 }]}
        >
          <TextArea  placeholder="请输入" showCount maxLength={255}  />
        </FormItem>
        <FormItem
          label="上级"
          // name="parentId"
          rules={[{ required: true, message: '请选择上级！' }]}
        >
          <TreeSelect
            style={{ width: '100%' }}
            dropdownStyle={{ maxHeight: 400, overflow: 'auto',color:'white' }}
            treeData={treeData}
            placeholder="请选择上级区域"
            
            defaultValue={parentId}
            onChange={changeParent}
            treeDefaultExpandAll
          />
        </FormItem>
       
        <FormItem
          label="启用状态"
          name="useState"
        >
          <Radio.Group>
          
            <Radio value={0}>
              不启用
            </Radio>
            <Radio value={1} >
              启用
            </Radio>
          </Radio.Group>
        </FormItem>
      </Form>
    </Modal>
  );
};

export default CreateForm;
